﻿using System.Windows;
using System.Windows.Controls;

namespace WPNextTools.NControls
{
    [TemplatePart(Name = "PART_InnerPasswordBox", Type = typeof(PasswordBox))]
    public class NWatermarkPasswordBox : Control
    {
        public event RoutedEventHandler GotFocus = delegate { };
        public event RoutedEventHandler LostFocus = delegate { };

        #region Fields

        private Border _enableBorder;
        private PasswordBox _passwordBox;

        #endregion

        #region Constructor

        public NWatermarkPasswordBox()
        {
          
            this.DefaultStyleKey = typeof(NWatermarkPasswordBox);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_enableBorder != null)
            {
                _enableBorder.Tap += EnableBorderTap;
            }
            if (_passwordBox != null)
            {
                _passwordBox.GotFocus -= PasswordBoxGotFocus;
                _passwordBox.LostFocus -= PasswordBoxLostFocus;
                _passwordBox.PasswordChanged -= PasswordBoxPasswordChanged;
            }

            _enableBorder = GetTemplateChild("EnabledBorder") as Border;
            _enableBorder.Tap += EnableBorderTap;

            _passwordBox = GetTemplateChild("PART_InnerPasswordBox") as PasswordBox;
            _passwordBox.GotFocus += PasswordBoxGotFocus;
            _passwordBox.LostFocus += PasswordBoxLostFocus;

            _passwordBox.PasswordChanged += PasswordBoxPasswordChanged;

            if (_enableBorder != null)
            {
                DetermineWatermarkContentVisibility();
            }
        }

        void PasswordBoxPasswordChanged(object sender, RoutedEventArgs e)
        {
            this.RealPassword = _passwordBox.Password;
        }

        void EnableBorderTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _passwordBox.Focus();
        }

        void PasswordBoxGotFocus(object sender, RoutedEventArgs e)
        {
            if (_enableBorder != null && string.IsNullOrEmpty(_passwordBox.Password.ToString()))
            {
                _enableBorder.Visibility = Visibility.Collapsed;
            }
            if (this.GotFocus != null)
            {
                this.GotFocus.Invoke(this, e);
            }
        }

        void PasswordBoxLostFocus(object sender, RoutedEventArgs e)
        {
            if (_enableBorder != null && string.IsNullOrEmpty(_passwordBox.Password.ToString()))
            {
                _enableBorder.Visibility = Visibility.Visible;
            }
            if (this.LostFocus != null)
            {
                this.LostFocus.Invoke(this, e);
            }
        }

        #endregion

        #region DepdencyProperties

        #region Watermark

        public object Watermark
        {
            get { return (object)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }

        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.Register("Watermark", typeof(object), typeof(NWatermarkPasswordBox), new PropertyMetadata(OnWatermarkPropertyChanged));

        private static void OnWatermarkPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NWatermarkPasswordBox watermarkPasswordBox = d as NWatermarkPasswordBox;
            if (watermarkPasswordBox != null && watermarkPasswordBox._enableBorder != null)
            {
                watermarkPasswordBox.DetermineWatermarkContentVisibility();
            }
        }

        #endregion

        #region WatermarkStyle

        public Style WatermarkStyle
        {
            get { return ( Style)GetValue(WatermarkStyleProperty); }
            set { SetValue(WatermarkStyleProperty, value); }
        }

        public static readonly DependencyProperty WatermarkStyleProperty =
            DependencyProperty.Register("WatermarkStyle", typeof( Style), typeof(NWatermarkPasswordBox), null);
        
        #endregion

        public string RealPassword
        {
            get { return (string)GetValue(RealPasswordProperty); }
            set { SetValue(RealPasswordProperty, value); }
        }

        public static readonly DependencyProperty RealPasswordProperty =
            DependencyProperty.Register("RealPassword", typeof(string), typeof(NWatermarkPasswordBox), new PropertyMetadata(string.Empty,OnRealPasswordChanged));

        private static void OnRealPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        #endregion

        #region Method

        private void DetermineWatermarkContentVisibility()
        {
            if (string.IsNullOrEmpty(_passwordBox.Password.ToString()))
            {
                _enableBorder.Visibility = Visibility.Visible;
            }
            else
            {
                _enableBorder.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

    }
}
